package com.xander.myblog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xander.myblog.constant.Constants;
import com.xander.myblog.entity.Article;
import com.xander.myblog.mapper.ArticleMapper;
import com.xander.myblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author hbxz
 * @date 2018/12/19 0019
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void insertArticle(Article article) {
        article.setCreateTime(new Date());
        articleMapper.insertArticle(article);
    }

    @Override
    public void updateArticle(Article article) {
        article.setUpdateTime(new Date());
        articleMapper.updateArticle(article);
    }

    @Override
    public PageInfo<Article> getArticleListByTid(Long tid, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Article> list = articleMapper.getArticleListByTid(tid);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Article> getAllArticle(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Article> list = articleMapper.getAllArticle();
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Article> getArticleListByAuthorId(Long authorId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Article> list = articleMapper.getArticleListByAuthorId(authorId);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void deleteArticleByAid(Article article) {
        article.setUpdateTime(new Date());
        article.setDelFlag(Constants.IS_DELETED);
        articleMapper.updateArticle(article);
    }

    @Override
    public PageInfo<Article> getArticleListByDate(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Article> list = articleMapper.getArticleListByDate();
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Article getArticleByAid(Long aid) {
        if(aid==null){
            return null;
        }
        return articleMapper.getArticleByAid(aid);
    }
}
